package com.siecom.framework.channel;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Mutex  
{  
    private boolean syncLock;  
    private Object lock;
    //private Lock lock;
      
    ////////////////////////////////////////////////  
    //  Constructor  
    ////////////////////////////////////////////////  
  
    public Mutex()  
    {  
        syncLock = false;  
    	lock = new ReentrantLock();
    }  
      
    ////////////////////////////////////////////////  
    //  lock  
    ////////////////////////////////////////////////  
      
    public synchronized void lock()  
    {  
    	/*
    	synchronized (lock){
    		if(syncLock == true){
	    		try {
					//�ȴ�����
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
    		}
    		syncLock = true;  
    	}
    	*/
    	
    	
    	
        while(syncLock == true) {  
            try {  
                wait();
            }  
            catch (Exception e) {  
                //Debug.warning(e);  
            }; 
        }  
        syncLock = true;  
    //	if(lock != null){
   // 	  	lock.lock();
    //	}
  
    }  
  
    public synchronized void unlock()  
    {  
    	
    	//synchronized (lock){
    	//	syncLock = false;
    	//	lock.notify();
    	//}
    	syncLock = false;
    	notify();
    	//if(lock != null){
   //     	lock.unlock();
    	//}
    }  
  

}  